package com.ysl.netty.webserver;

import cn.hutool.core.util.StrUtil;
import com.ysl.netty.common.SessionSocket;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

public class WebServerHandler extends SimpleChannelInboundHandler<Object> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
        //System.out.println(ctx.channel().remoteAddress());
        //System.out.println(ctx.channel().id());
        String message="";
        if(msg instanceof TextWebSocketFrame)
        {
            message=((TextWebSocketFrame)msg).text();
            System.out.println(message);
        }
        else if(msg instanceof BinaryWebSocketFrame)
        {
            byte[] bytes=((BinaryWebSocketFrame)msg).content().array();
        }
        else
        {
            System.out.println("未知消息"+msg.getClass().getName());
        }

        /*List<String> listclient = SocketChannelGroup.likeRedisTemplate.getOnline();
        for (int i = 0; i < listclient.size(); i++) {
            String cid=listclient.get(i);

            Channel channel= SocketChannelGroup.likeRedisTemplate.getChannel(cid);
            System.out.println(channel);

            channel.writeAndFlush(message);
        }*/

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        //可能出现业务判断离线后再次触发 channelInactive
        String userid = SessionSocket.likeRedisTemplate.getUserId(ctx.channel());
        if (StrUtil.isNotEmpty(userid)){
            SessionSocket.likeRedisTemplate.deleteChannel(userid);
            ctx.channel().close();
        }
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
            if (idleStateEvent.state() == IdleState.READER_IDLE) {

            }
        }
        super.userEventTriggered(ctx, evt);
    }

    /**
     * 	出现异常的情况下怎么办
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        Channel ch = ctx.channel();
        //GlobalChannelGroup.channelGroup.add(ch);
        //SocketChannelGroup.likeRedisTemplate.saveChannel(ch.id().asLongText(),ch);

        //System.out.println(SessionSocket.likeRedisTemplate.getOnline());
        super.handlerAdded(ctx);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        Channel ch = ctx.channel();
        String userid = SessionSocket.likeRedisTemplate.getUserId(ctx.channel());
        if(StrUtil.isNotEmpty(userid))
        {
            SessionSocket.likeRedisTemplate.deleteChannel(userid);
        }
        super.handlerRemoved(ctx);
    }
}
